var app = getApp();
var type;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_list:[
      {
        "type": "text",
        "title": "姓名",
        "name": "name"
      },
      {
        "type": "number",
        "title": "联系电话",
        "name": "tel"
      },
      {
        "type": "top"
      },
      {
        "type": "pic",
        "title": "头像",
        "name": "headimg",
        "num": 1,
        "pics": []
      },
      {
        "type": "top"
      },
      {
        "type": "textarea",
        "title": "详细介绍",
        "name": "content"
      },
      {
        "type": "top"
      },
      {
        "type": "pic",
        "title": "介绍图片",
        "name": "pics",
        "num": 2,
        "pics": []
      },
    ]
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              console.log(out);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                list.forEach(function (value, index) {
                  console.log(value);
                  console.log(data);
                  if (value.title == data.title) {
                    var pics = list[index].pics;
                    var file_name = out.data.domain;
                    if (data.num > 1) {
                      pics.push(file_name);
                    } else {
                      pics = [file_name];
                    }
                    list[index].pics = pics;
                  }
                })
                that.setData({
                  input_list: list,
                  token: token
                })
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  formSubmit: function(e){
    app.addFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var post_data = e.detail.value;
    var data = this.data;
    data.input_list.forEach(function (item, idx) {
      if (item.type == 'pic') {
        post_data[item.name] = item.pics;
        post_data.img_url = item.pics[0];
      }
    })
    post_data.openid = app.globalData.openid;
    post_data.type = type;
    app.ihttpPost("https://www.58zltc.com/api.php/Jiaoyu/memberAdd", post_data, res=>{
      if (res.data.err_code > 0) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        wx.showToast({
          title: res.data.err_msg,
          icon: 'success'
        })
        setTimeout(function () {
          wx.navigateBack({})
        }, 500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    type = options.type;
    this.setData({
      type: type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  backto: function () {
    wx.navigateBack({
      
    })
  },
  delImg: function (e) {
    let that = this;
    var flash = that.data.input_list;
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.num;
    var img_path = e.currentTarget.dataset.path;
    var openid = app.globalData.openid;
    var type = that.data.type;
    var flashIdx = 0;
    for (var i = 0; i < flash.length; i++) {
      if (flash[i].pics || flash[i].pics == []) {
        if (flash[i].num == num) {
          flash[i].pics.splice(index, 1);
          that.setData({
            input_list: flash
          })
        }
      }
    }  
    wx.showToast({
      title: '删除成功',
      icon: 'success',
      duration: 1000
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop/delShopJiaoYuMem?token=' + that.data.token,
      data: {
        openid: openid,
        path: img_path,
        type: type,
        num: num
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: function (res) {
        if (!res.data) {
          wx.showToast({
            title: '网络繁忙',
            icon: 'none',
            duration: 1500
          })
        }
      }
    })
  }
})